﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Model;
using BLL;
using Utility;
using System.IO;

public partial class VehicleUI_CheckScore_CheckScoreAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Type"] == null)
            {
                if (Session["FilterFrameNO"] != null)
                {
                    txt_FrameNO.Text = Session["FilterFrameNO"].ToString();
                }
            }
           else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_VehicleID.Enabled = false;
                txt_CheckDate.Enabled = false;
                txt_FrameNO.Enabled = false;
                txt_Score.Enabled = false;
                ful_CHK.Visible = false;
                btn_Sure.Visible = false;
            }
           else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }

    protected void LoadService()
    {
        int _id = Convert.ToInt32(Request.QueryString["ID"]);
        CheckScore cs = new CheckScoreBLL().GetModel(_id);
        if (cs != null)
        {
            txt_VehicleID.Text = cs.VehicleID;
            txt_FrameNO.Text = cs.FrameNO;
            txt_CheckDate.Text = cs.CheckDate.ToString();
            txt_Score.Text = cs.Score.ToString();

            if (cs.CheckImage != string.Empty)
            {
                img_CHK.ImageUrl = GlobalArgs.CheckScorePath + cs.CheckImage;
            }
        }
    }


    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        string _frameNO = txt_FrameNO.Text.Trim();
        bool reExist = new VehicleBLL().Exists(_frameNO);
        if (!reExist)
        {
            UtilityService.Alert(this.Page, "不存在该车辆信息,请重新录入车架号!");
            txt_FrameNO.Focus();
            return;
        }

        if (!ValidateService.IsNumber(txt_Score.Text.Trim()))
        {
            UtilityService.Alert(this.Page, "得分格式错误!");
            txt_Score.Focus();
            return;
        }

        if (Request.QueryString["Type"] == null)
        {
            CheckScore cs = new CheckScore();

            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.CheckDate = DateTime.Parse(txt_CheckDate.Text.Trim());
            cs.Score = int.Parse(txt_Score.Text.Trim());
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

 
            string f2 = UploadFile(ful_CHK, 1);
            cs.CheckImage = f2.Length > 5 ? f2 : "";

            int re = new CheckScoreBLL().Add(cs);

            if (re > 0)
            {
                string msg = "";

                if (f2 == "err")
                {
                    msg += "检查图片上传失败!";
                }
                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "CheckScoreMgr.aspx");
            }
            else
            {
                FileInfo file2 = new FileInfo(Server.MapPath(GlobalArgs.CheckScorePath) + f2);
                if (file2.Exists)
                {
                    file2.Delete();
                }
                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            int _id = Convert.ToInt32(Request.QueryString["ID"]);
            CheckScore cs = new CheckScoreBLL().GetModel(_id);
            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.CheckDate = DateTime.Parse(txt_CheckDate.Text.Trim());
            cs.Score = int.Parse(txt_Score.Text.Trim());

            string f2 = "";
            if (ful_CHK.HasFile)
            {
                f2 = UploadFile(ful_CHK, 1);
                cs.CheckImage = f2.Length > 5 ? f2 : "";
            }

            int re = new CheckScoreBLL().Update(cs);

            if (re > 0)
            {
                string msg = "";

                if (f2 == "err")
                {
                    msg += "检查图片上传失败!";
                }
                UtilityService.AlertAndRedirect(this.Page, "修改成功! " + msg, "CheckScoreMgr.aspx");
            }
            else
            {

                FileInfo file2 = new FileInfo(Server.MapPath(GlobalArgs.CheckScorePath) + f2);
                if (file2.Exists)
                {
                    file2.Delete();
                }

                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }

    public string UploadFile(FileUpload ful,int type)
    {

        try
        {

            if (!ful.HasFile)
            {
               // this.Upload_info.Text = "请选择上传文件！";
                return "none";
            }
            else
            {
                string filepath = ful.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\My Pictures\20022775_m.jpg 
                string filename = "";
                string ext = filepath.Substring(filepath.LastIndexOf("."));
                if (type == 0)
                {
                    
                    filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_gps" +ext;//取得文件在服务器上保存的位置C:\Inetpub\20022775_m.jpg
                }
                else
                {
                    filename = Session["OrganID"].ToString() + "_" + DateTime.Now.Ticks.ToString() + "_chk" + ext;
                }

                string serverpath = Server.MapPath(GlobalArgs.CheckScorePath) + filename;//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg

                ful.PostedFile.SaveAs(serverpath);//将上传的文件另存为 
                //this.Upload_info.Text = "上传成功！";
                return filename;
            }
        }
        catch (Exception ex)
        {
            UtilityService.Alert(this.Page,"上传发生错误!原因是:" + ex.ToString());
            //this.Upload_info.Text = "上传发生错误！原因是：" + ex.ToString();
            return "err";
        }

    }

    protected void btn_Sel1_Click(object sender, EventArgs e)
    {

    }
    protected void btn_Sel_Click(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        UtilityService.Open(this.Page, "../../ImageView.aspx?url=" + ((ImageButton)sender).ImageUrl);
    }
    protected void btn_Back_Click(object sender, EventArgs e)
    {
       Response.Redirect("CheckScoreMgr.aspx");
    }
}
